package cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords.vo.TranslationTaskRecordsSaveReqVO;
import cn.iocoder.yudao.module.dify.service.translationtaskrecords.TranslationTaskRecordsService;
import io.github.imfangs.dify.client.exception.DifyApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - dify翻译工作流任务记录")
@RestController
@RequestMapping("/dify/translation-task-records")
@Validated
public class TranslationTaskUseDifyController {

    @Resource
    private TranslationTaskRecordsService translationTaskRecordsService;

    @PostMapping(path = "/translateWorkflow/upload/create")
    @Operation(summary = "翻译工作流-上传文件-创建任务")
    public CommonResult<Long> translateWorkflowUploadCreate(@RequestParam("file") MultipartFile file) throws DifyApiException, IOException, InterruptedException {
        return CommonResult.success(translationTaskRecordsService.translateWorkflowUploadCreate(file, getLoginUserId()));
    }

    @PostMapping(path = "/translateWorkflow/batchUpload/create")
    @Operation(summary = "翻译工作流-多文件-创建任务")
    public CommonResult<Long> translateWorkflowCreateTask(@RequestBody @Valid TranslationTaskRecordsSaveReqVO createReqVO) throws IOException {
        return CommonResult.success(translationTaskRecordsService.translateWorkflowCreateTask(createReqVO.getFileUrls(), getLoginUserId()));
    }

    @PostMapping(path = "/translateWorkflow/executeTask", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "翻译工作流-上传文件-执行任务")
    public CommonResult<Boolean> translateWorkflowExecuteTask(@RequestParam("id") Long id) throws DifyApiException, IOException, InterruptedException {
        return CommonResult.success(translationTaskRecordsService.startTranslateTaskById(id));
    }

    @PostMapping(path = "/translateWorkflow/upload/createTask2")
    @Operation(summary = "翻译工作流-上传文件到miniIO-创建任务2")
    public CommonResult<Long> translateWorkflowUploadCreateTask(@RequestParam("file") MultipartFile file) throws DifyApiException, IOException, InterruptedException {
        return CommonResult.success(translationTaskRecordsService.translateWorkflowUploadCreateTask(file, getLoginUserId()));
    }

    @PostMapping(path = "/translateWorkflow/splitFile/start")
    @Operation(summary = "翻译工作流-任务id-执行任务")
    public CommonResult<Boolean> translateWorkflowTextExecuteTask(@RequestParam("id") Long id) {
        return CommonResult.success(translationTaskRecordsService.translateWorkflowSplitFileById(id));
    }

    @PostMapping("/create")
    @Operation(summary = "创建dify翻译工作流任务记录")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:create')")
    public CommonResult<Long> createTranslationTaskRecords(@Valid @RequestBody TranslationTaskRecordsSaveReqVO createReqVO) {
        return success(translationTaskRecordsService.createTranslationTaskRecords(createReqVO));
    }


}